package com.key.puzzlemodel.transverselongitudinal.fragment

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import com.key.puzzlemodel.R
import com.key.puzzlemodel.base.BaseMenuFragment
import com.key.puzzlemodel.databinding.FragmentBorderBinding
import com.key.puzzlemodel.transverselongitudinal.IPuzzleInterFace
import com.key.puzzlemodel.transverselongitudinal.TransverseLongitudinalMVVMViewModel

/**
 *
 * @功能描述:
 * @创建时间:     2022/1/7
 * @创建者:       阳
 * @修改时间:     2022/1/7
 * @修改者:       阳
 *
 */
class BorderFragment() : BaseMenuFragment<FragmentBorderBinding, TransverseLongitudinalMVVMViewModel>(),
    View.OnClickListener {

    override val layoutId: Int
        get() = R.layout.fragment_border

    var borderProgress = 0


    //临时变量
    var tabBorderInside: Int = 0
    var tabBorderOutside: Int = 0
    var tabBorderRound: Int = 0

    override fun onAttach(context: Context) {
        super.onAttach(context)
        puzzleInterface = context as IPuzzleInterFace
    }

    /**
     *
    1、内测：调整每张图片边框的大小，等比缩放，默认值为0，最大值为50。
    2、外侧：图片与图片之间的间距不变，调整画布与所有图片的外侧间距，默认值为0，最大值为50。
    3、圆角：调整每张图片四角的弧度，默认值为0（直角），最大值为50。
    边框板块可参考一键抠图中的一键拼。
    4、所有底部编辑弹窗点击“×”收起弹窗不保留弹窗里的操作，点击“✔”收起弹窗并保留弹窗里的操作。
     *
     */
    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
        //设置默认值
        clearBorder()

        mVDB.seekBorder.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                borderProgress = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                // 监听停止拖动滚动条的操作
                when {
                    mVDB.tabBorderInside.isChecked -> {
                        //内边距
                        tabBorderInside = borderProgress
                        puzzleInterface.clickUpdateBorderInside(tabBorderInside)
                    }
                    mVDB.tabBorderOutside.isChecked -> {
                        //外边距
                        tabBorderOutside = borderProgress
                        puzzleInterface.clickUpdateBorderOutside(tabBorderOutside)
                    }
                    mVDB.tabBorderRound.isChecked -> {
                        //圆角
                        tabBorderRound = borderProgress
                        puzzleInterface.clickUpdateBorderRound(tabBorderRound)
                    }
                }
            }
        })
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.img_border_close -> {
                puzzleInterface.clickUpdateBorderInside(mVM.borderInsideNumber)
                puzzleInterface.clickUpdateBorderOutside(mVM.borderOutsideNumber)
                puzzleInterface.clickUpdateBorderRound(mVM.borderRoundNumber)

                clearBorder()
                puzzleInterface.clickStartFragment(mVM.MENUTYPE)
            }
            R.id.img_border_ok -> {

                mVM.borderInsideNumber = tabBorderInside
                mVM.borderOutsideNumber = tabBorderOutside
                mVM.borderRoundNumber = tabBorderRound

                clearBorder()
                puzzleInterface.clickStartFragment(mVM.MENUTYPE)
            }
            R.id.tab_border_inside -> {
                //内边距
                borderProgress = if (tabBorderInside != 0) {
                    tabBorderInside
                } else {
                    mVM.borderInsideNumber
                }
                mVDB.seekBorder.progress = borderProgress
            }
            R.id.tab_border_outside -> {
                //外边距
                borderProgress = if (tabBorderOutside != 0) {
                    tabBorderOutside
                } else {
                    mVM.borderOutsideNumber
                }
                mVDB.seekBorder.progress = borderProgress
            }
            R.id.tab_border_round -> {
                //圆角
                borderProgress = if (tabBorderRound != 0) {
                    tabBorderRound
                } else {
                    mVM.borderRoundNumber
                }
                mVDB.seekBorder.progress = borderProgress
            }
        }
    }

    /**
     * 清理临时变量
     */
    private fun clearBorder() {
        borderProgress = mVM.borderInsideNumber

        tabBorderInside = mVM.borderInsideNumber
        tabBorderOutside = mVM.borderOutsideNumber
        tabBorderRound = mVM.borderRoundNumber

        mVDB.seekBorder.progress = borderProgress
        mVDB.tabBorderInside.isChecked = true
    }


}